import jieba as jb
file=open("test.txt","r",encoding="UTF-8").read()#必须确保源文件是utf-8格式
out=open("out.txt","a+",encoding="UTF-8") #追加写入
words=jb.lcut(file)
counts={}
for word in words:
    if len(word)==1:
        continue
    else:
        counts[word]=counts.get(word,0)+1
items=list(counts.items())
# print(items)
items.sort(key=lambda x:x[1],reverse=True)
for i in range(5):
    word,count=items[i]
    #print("{0}->{1}".format(word,count))
    out.write("{0}->{1}\n".format(word,count))
print("out.txt导出成功")
out.close()
